/*
 *  WCockpitRenderer.cpp
 *  GLRender
 *
 *  Created by Teseo on 22.05.10.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

#include "WCockpitRenderer.h"
#include "Player.h"
#include "Configuration.h"

void
WCockpitRenderer::initialize()
{
	SQUARE_DL= initSquare();
	
	
	cockpitTexture.bind("../../obj/cockpit.png");
	radarTexture.bind("../../obj/radar.png");
	radarTargetTexture.bind("../../obj/radarTarget.png");
	arrowTexture.bind("../../obj/arrow.png");
	centerTexture.bind("../../obj/center.png");
	
	horizontBackgroundTexture.bind("../../obj/horizontBackground.png");
	horizontMarkTexture.bind("../../obj/horizontMark.png");
	
	targetTexture.bind("../../obj/target.png");
	radarStrokeTexure.bind("../../obj/radarStroke.png");
	
	lifebarTexure.bind("../../obj/lifebar.png");
	lifebarBackgroundTexture.bind("../../obj/lifebarBackground.png");
	
	airplaneProfileTexture.bind("../../obj/airplaneProfile.png");
}

void
WCockpitRenderer::renderRadar(const Point3d &target)
{	
	glPushMatrix();
	
	glTranslated(-1.5,-1.22,0);
	glScaled(0.25, 0.25, 0.25);
	
	//RADAR
	glColor4d(1.0,1.0,1.0, 0.6);
	radarTexture.enable();
	radarTexture.bind();
	glCallList(SQUARE_DL);
	radarTexture.disable();
	
	//TARGET
	glPushMatrix();
	radarTargetTexture.enable();
	radarTargetTexture.bind();
	glTranslated(target.x(), target.y(), 0);
	glScaled(0.1, 0.1, 0.1);
	glCallList(SQUARE_DL);
	radarTargetTexture.disable();
	glPopMatrix();
	
	//RADAR STROKE
	glRotated(tau++, 0, 0, 1);
	radarStrokeTexure.enable();
	radarStrokeTexure.bind();
	glCallList(SQUARE_DL);
	radarStrokeTexure.disable();
	
	
	
	glPopMatrix();
}

void
WCockpitRenderer::renderLifebar(double life, double maxLife){
	
	const double scale=0.3;
	glPushMatrix();
	glTranslated(-0.5, -1.1, 0);
	glColor4d(1.0,1.0,1.0, 0.8);
	
	glScaled(scale, scale, scale);
	lifebarBackgroundTexture.enable();
	lifebarBackgroundTexture.bind();
	glCallList(SQUARE_DL);
	lifebarBackgroundTexture.disable();
	
	const double healt=life<=0?0:life/maxLife;
	glTranslated(healt-1.0, 0, 0);
	glScaled(healt, 1, 1);
	lifebarTexure.enable();
	lifebarTexure.bind();
	glCallList(SQUARE_DL);
	lifebarTexure.disable();
	
	glPopMatrix();
	
}

void
WCockpitRenderer::renderTarget(){
	glPushMatrix();
	const double scale=0.4;
	glScaled(scale,scale,scale);
	glColor4d(1.0,1.0,1.0, 0.3);
	targetTexture.enable();
	targetTexture.bind();
	glCallList(SQUARE_DL);
	targetTexture.disable();

	glPopMatrix();
}

void WCockpitRenderer::renderTargetArrow(const Point3d &where){
	//const double factor=180.0/3.1416;
	//const double alpha=where.getAngle(Point3d(1,0,0))*factor;
	//const double beta=where.getAngle(Point3d(0,1,0))*factor;
	//const double gamma=where.getAngle(Point3d(0,0,1))*factor;
	
	glPushMatrix();
	//const double scale=0.4;
	const double blow=1.0;
	
	
	//glRotated(alpha, 1, 0, 0);
	//glRotated(beta, 0, 1, 0);
	//glRotated(gamma, 0, 0, 1);
	
	//glLoadIdentity();
	glTranslated(0, 0, -blow);
	
	
	
	glColor4d(1.0,0,0, 1.0);
	glBegin(GL_TRIANGLES);
	glVertex3d(-0.5, 0, 0);
	glVertex3d(0.5, 0, 0);
	glVertex3d(-where.x()*blow,-where.y()*blow,where.z()*blow);
	glEnd();
	
	glColor4d(0,0,1, 1.0);
	glPointSize(10.0f);
	glBegin(GL_POINTS);
	glVertex3d(-where.x()*blow,-where.y()*blow,where.z()*blow);
	glEnd();
	
	glColor4d(0.0,1,0, 0.2);
	GLUquadricObj *sphere;
	sphere = gluNewQuadric();
	gluQuadricDrawStyle( sphere, GLU_LINE );
	
	
	glRotated(30, 1, 1, 1);
	gluSphere( sphere, 1.0, 20,10 );
	
	//glScaled(scale,scale,scale);
	//arrowTexture.enable();
	//arrowTexture.bind();
	//glCallList(SQUARE_DL);
	//arrowTexture.disable();
	
	glPopMatrix();
}

void
WCockpitRenderer::renderAltimeter(const double altitude)
{
	const double tol = 0.2;
	const double scale=0.06;
	const double offAlpha=0.3, onAlpha=1.0;
	const double center=(-tol<=altitude && altitude<=tol)?onAlpha:offAlpha;
	const double up=(altitude>tol)?onAlpha:offAlpha;
	const double down=(-tol>altitude)?onAlpha:offAlpha;
	
	glPushMatrix();
	glTranslated(-1, -1.15, 0);
	glScaled(scale,scale,scale);
	glColor4d(1.0,1.0,1.0, up);
	arrowTexture.enable();
	arrowTexture.bind();
	glCallList(SQUARE_DL);
	arrowTexture.disable();
	
	glTranslated(0, -1.7, 0);
	glColor4d(1.0,1.0,1.0, center);
	centerTexture.enable();
	centerTexture.bind();
	glCallList(SQUARE_DL);
	centerTexture.disable();
	
	glTranslated(0, -1.7, 0);
	glColor4d(1.0,1.0,1.0, down);
	glRotated(180, 0, 0, 1);
	arrowTexture.enable();
	arrowTexture.bind();
	glCallList(SQUARE_DL);
	arrowTexture.disable();
	
	
	glPopMatrix();
}

void
WCockpitRenderer::renderHorizontal(double alpha){
	const double scale=0.25;
	glPushMatrix();
	glTranslated(1.5, -1.25, 0);
	glScaled(scale,scale,scale);
	glColor4d(1.0,1.0,1.0, 0.8);
	horizontBackgroundTexture.enable();
	horizontBackgroundTexture.bind();
	glCallList(SQUARE_DL);
	horizontBackgroundTexture.disable();
	
	
	glRotated(alpha-90, 0, 0, 1);
	horizontMarkTexture.enable();
	horizontMarkTexture.bind();
	glCallList(SQUARE_DL);
	horizontBackgroundTexture.disable();

	
	glPopMatrix();
}

void
WCockpitRenderer::renderVertical(double alpha){
	const double scale=0.25;
	glPushMatrix();
	glTranslated(0.5, -1.25, 0);
	glScaled(scale,scale,scale);
	glColor4d(1.0,1.0,1.0, 0.8);
	
	
	glRotated(alpha, 0, 0, 1);
	airplaneProfileTexture.enable();
	airplaneProfileTexture.bind();
	glCallList(SQUARE_DL);
	airplaneProfileTexture.disable();

	
	glPopMatrix();
}

void
WCockpitRenderer::renderCockpit(){	
	glPushMatrix();
	//glRotated(270, 0, 0, 1);
	glScaled(getWidth(), getHeight(), 1.0); //2.12
	
	glColor3f(1.0f, 1.0f, 1.0f);
	cockpitTexture.enable();
	cockpitTexture.bind();
	
	glCallList(SQUARE_DL);
	
	
	
	cockpitTexture.disable();
	
	glPopMatrix();
	
}

void
WCockpitRenderer::renderPlayer(Player *player){
	setDimensions(2.6, 1.5);
	
	glDisable( GL_LIGHTING );
	glEnable (GL_BLEND); 
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glPushMatrix();
	glLoadIdentity();
	glTranslated(0, 0, -1.5);
	
	if (Configuration::getInstance()->hasToShowCockpit()) {
		renderCockpit();
	}
	
	renderTarget();
	glPushMatrix();
	
	glTranslated(0, -0.6, 0);
	glScaled(0.5, 0.5, 0.5);
	renderRadar(player->getRadarTarget());
	renderAltimeter(player->getTargetAltitude());
	renderHorizontal(player->getAlpha());
	renderVertical(player->getVerticalAngle());
	
	
	renderLifebar(player->getLife(), player->getMaxLife());
	//renderTargetArrow(player->getTargetVector());
	
	glPopMatrix();
	renderFlash(flashColor);
	glPopMatrix();
	
	glDisable(GL_BLEND);
	glEnable( GL_LIGHTING );
}

GLuint
WCockpitRenderer::initSquare()
{
	GLuint targetDL = glGenLists(1);
	
	glNewList(targetDL, GL_COMPILE);
	
	glBegin(GL_QUADS);
	
	
	
	glTexCoord2d(0, 1);
	glVertex3f(-1.0f, 1.0f, 0.0f);				// Top Left
	
	glTexCoord2d(1,1);
	glVertex3f( 1.0f, 1.0f, 0.0f);				// Top Right
	
	glTexCoord2d(1,0);
	glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
	
	glTexCoord2d(0,0);
	glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
	
	glEnd();
	
	
	glEndList();
	
	if(!glIsList(targetDL))
	{
		printf("NO LIST FOR Square\n");
	}
	
	return targetDL;
}

void 
WCockpitRenderer::renderFlash(const RGBd &rgb)
{
	if (flashCounter <= 0) {
		return;
	}
	
	glPushMatrix();
	glScaled(getWidth(), getHeight(), 1.0); 
	
	glColor4d(rgb.r(), rgb.g(), rgb.b(), (double)flashCounter--/(double)(maxFlashCounter + 30.0));
	
	glCallList(SQUARE_DL);
	glPopMatrix();
	
	
}


void
WCockpitRenderer::startFlash(const RGBd &rgb, const int duration)
{
	maxFlashCounter = duration;
	flashColor = rgb;
	flashCounter = maxFlashCounter;	
}
